//
//  DropDownClass.m
//  DropDownMenuDome
//
//  Created by Bc_Ltf on 15/1/19.
//  Copyright (c) 2015年 Bc_Ltf. All rights reserved.
//

#import "DropDownView.h"

#import "MSFilterNormalCell.h"

#import "UIView+Extension.h"


static NSString *defaultIdentifier = @"DropDownMenuCell";

@interface DropDownView()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, copy) NSArray *dataSource; // 左边数据
@property (nonatomic, copy) NSArray *rightDataSource; // 右边的数据
@property (nonatomic, assign) MSFilterViewType viewType;

@property (nonatomic, assign) NSInteger selectFirstRow; // 当前选中的第一个row
@property (nonatomic, assign) NSInteger selectRightRow; // 当前选中的第二个row
@property (nonatomic, copy) NSDictionary *selectDict; // 选中的数据

@end

@implementation DropDownView

#pragma mark- 初始化 ：原点+高
- (instancetype)initWithFrame:(CGRect)frame data:(NSArray *)dataSource viewType:(MSFilterViewType)viewType {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.selectFirstRow = 0;
        self.selectRightRow = 0;
        if (viewType == MSFilterViewTypeOneTable) {
            self.rightDataSource = dataSource;
            self.selectDict = [dataSource firstObject];
        } else {
            self.dataSource = dataSource;
            if (self.dataSource.count > 0) {
                NSDictionary *firstDict = [self.dataSource firstObject];
                self.rightDataSource = [MSSafeObject ArrayInfo:firstDict[@"childs"]];
            }
        }
        self.viewType = viewType;
        [self createTableView:CGPointZero];
    }
    return self;
}

#pragma mark- 创建各种东东
- (void)createTableView:(CGPoint)origin {
    CGFloat leftTableViewWidth = MS_SCREEN_WIDTH;
    CGFloat rightTableViewWidth = MS_SCREEN_WIDTH;
    if (self.viewType == MSFilterViewTypeMoreTableEqualWidth) {
        leftTableViewWidth = 0.5 * MS_SCREEN_WIDTH;
        rightTableViewWidth = 0.5 * MS_SCREEN_WIDTH;
    } else if (self.viewType == MSFilterViewTypeMoreTableLessWidth) {
        leftTableViewWidth = 0.3 * MS_SCREEN_WIDTH;
        rightTableViewWidth = 0.7 * MS_SCREEN_WIDTH;
    }
    
    // 右栏
    _rightTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _rightTableView.rowHeight = 45;
    _rightTableView.dataSource = self;
    _rightTableView.delegate = self;
    _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_rightTableView registerNib:[UINib nibWithNibName:@"MSFilterNormalCell" bundle:nil] forCellReuseIdentifier:@"MSFilterNormalCell"];
    [self addSubview:_rightTableView];
    [_rightTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.offset(0);
        make.width.offset(rightTableViewWidth);
    }];
    
    if (self.viewType == MSFilterViewTypeOneTable) {
        return;
    }
    
    // 左栏
    _leftTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _leftTableView.rowHeight = 45;
    _leftTableView.dataSource = self;
    _leftTableView.delegate = self;
    [_leftTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:defaultIdentifier];
    _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (self.viewType != MSFilterViewTypeOneTable &&
        self.viewType != MSFilterViewTypeFilter) {
        _leftTableView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
    }
    
    [self addSubview:_leftTableView];
    [_leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.offset(0);
        make.width.offset(leftTableViewWidth);
    }];
    
}

#pragma mark - table datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//        return [self menu:self tableView:tableView numberOfRowsInSection:section];
    if (tableView == self.leftTableView) {
        // lefttableview只有在多个tableview时才会出来
        return self.dataSource.count;
    }
    return self.rightDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.leftTableView == tableView) {
        // 左边的tableView，用系统默认的cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:defaultIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone; // 无选中状态
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
        cell.backgroundColor = [UIColor clearColor]; // 背景色默认为透明
        if (self.selectFirstRow == indexPath.row) {
            cell.contentView.backgroundColor = [UIColor whiteColor]; // 背景色默认为透明
        } else {
            cell.contentView.backgroundColor = [UIColor clearColor];
        }
        
        NSDictionary *dict = self.dataSource[indexPath.row];
        NSString *textName = @"";
        if ([[dict allKeys] containsObject:@"title"]) {
            textName = [MSSafeObject stringInfo:dict[@"title"]];
        } else {
            textName = [MSSafeObject stringInfo:dict[@"name"]];
        }
        cell.textLabel.text = textName;
        
        return cell;
    }
    // 右边的cell
    MSFilterNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"MSFilterNormalCell"];
    if (indexPath.row < self.rightDataSource.count) {
        NSDictionary *dict = self.rightDataSource[indexPath.row];
        [normalCell refreshFilterNormalCellWithDictionary:dict];
        if ([self.selectDict isEqualToDictionary:dict]) {
            self.selectRightRow = indexPath.row;
            [normalCell changeCellSelectStatus:YES];
        } else {
            [normalCell changeCellSelectStatus:NO];
        }
        
        return normalCell;
    }
    return [UITableViewCell alloc];
}

#pragma mark - tableview delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    NSDictionary *selectDic = @{};
    if (self.leftTableView == tableView) {
        if (indexPath.row >= self.dataSource.count) {
            return;
        }
        if (self.selectFirstRow == indexPath.row) {
            return;
        }
        selectDic = [MSSafeObject dictionaryInfo:self.dataSource[indexPath.row]];
        if (self.selectFirstRow >= 0) {
            UITableViewCell *defaultCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectFirstRow inSection:0]];
            defaultCell.contentView.backgroundColor = [UIColor clearColor];
        }
        
        self.selectFirstRow = indexPath.row;
        // 将之前选中的cell状态改变
        UITableViewCell *oldCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectFirstRow inSection:0]];
        oldCell.contentView.backgroundColor = [UIColor clearColor];
        // 将新选中的cell状态改变
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.contentView.backgroundColor = [UIColor whiteColor];
        
        // 点击的昨天的tableView
        NSArray *selectArr = [MSSafeObject ArrayInfo:selectDic[@"childs"]];
        self.rightDataSource = selectArr;
        self.selectRightRow = -1;
        if (selectArr.count > 0) {
            // 修改右边的数据源
            [self.rightTableView reloadData];
            return;
        }
    } else {
        if (indexPath.row >= self.rightDataSource.count) {
            return;
        }
        selectDic = [MSSafeObject dictionaryInfo:self.rightDataSource[indexPath.row]];
        // 是rightTableView，说明cell有选中状态
        if (self.selectRightRow >= 0) {
            MSFilterNormalCell *oldSelectCell = (MSFilterNormalCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectRightRow inSection:0]];
            [oldSelectCell changeCellSelectStatus:NO];
        }
        
        MSFilterNormalCell *normalCell = (MSFilterNormalCell *)[tableView cellForRowAtIndexPath:indexPath];
        [normalCell changeCellSelectStatus:YES];
        self.selectRightRow = indexPath.row;
    }
    // 选中了某个数据
    self.selectDict = selectDic;
    if (self.SelectBlock) {
        self.SelectBlock(selectDic);
    }
}



#pragma mark - Public Method

- (void)hiddenView {
    self.height = 0.0;
}

@end
